﻿using LiSAClient.Core.Models;
using LiSAClient.WpfClient.View.Frame;
using LiSAClient.WpfClient.ViewModel.Frame.Menus;

namespace LiSAClient.WpfClient.ViewModel.Frame.Windows
{
    using System.Collections.Generic;
    using LiSAClient.Core.Interfaces;
    using LiSAClient.Core.Viewmodel;

    using GalaSoft.MvvmLight;

    public class DetachedWindowViewModel : BasePresenter
    {
        private const double MinWidth = 400;

        private const double MinHeight = 600;

        private double windowHeight;

        public double WindowHeight
        {
            get
            {
                return this.windowHeight;
            }

            set
            {
                // sets and notifies of update if value has changed
                this.Set(() => this.WindowHeight, ref this.windowHeight, value);
            }
        }

        private double windowWidth;

        public double WindowWidth
        {
            get
            {
                return this.windowWidth;
            }

            set
            {
                // sets and notifies of update if value has changed
                this.Set(() => this.WindowWidth, ref this.windowWidth, value);
            }
        }

        private double minWindowHeight;

        public double MinWindowHeight
        {
            get
            {
                return this.windowHeight;
            }

            set
            {
                // sets and notifies of update if value has changed
                this.Set(() => this.MinWindowHeight, ref this.minWindowHeight, value);
            }
        }

        private double minWindowWidth;

        public double MinWindowWidth
        {
            get
            {
                return this.windowWidth;
            }

            set
            {
                // sets and notifies of update if value has changed
                this.Set(() => this.MinWindowWidth, ref this.minWindowWidth, value);
            }
        }

        public DetachedWindowMenuViewModel TopMenu { get; set; }

        private MetaInfoBarViewModel metaInfoBar;

        public MetaInfoBarViewModel MetaInfoBar
        {
            get
            {
                return this.metaInfoBar;
            }

            private set
            {
                this.Set(() => this.MetaInfoBar, ref metaInfoBar, value);
            }
        }

        private string title;

        public string Title
        {
            get
            {
                return this.title;
            }

            private set
            {
                this.title = value;
                this.RaisePropertyChanged("Title");
            }
        }

        public DetachedWindowViewModel(string title)
        {
            this.Title = title;

            this.MinWindowHeight = MinHeight;
            this.MinWindowWidth = MinWidth;

            this.MetaInfoBar = new MetaInfoBarViewModel();
            this.TopMenu = new DetachedWindowMenuViewModel();
        }

        public override void StartWaiting(string waitMessage)
        {
            this.MinWindowHeight = this.WindowHeight;
            this.MinWindowWidth = this.WindowWidth;
            this.TopMenu.HideMenus = true;
            base.StartWaiting(waitMessage);
        }

        public override void FinishedWaiting()
        {
            base.FinishedWaiting();
            this.TopMenu.HideMenus = false;
            this.MetaInfoHandle(CurrentContent);
        }

        public override void PresentContent(ContentViewModel content, bool performOnPresented)
        {
            base.PresentContent(content, performOnPresented);
            if (!content.ShowInWindow)
            {
                this.MetaInfoHandle(content);
                this.TopMenu.ButtonChecks(content);
            }
        }

        public override void PresentContent(ContentViewModel content)
        {
            base.PresentContent(content);
            if (!content.ShowInWindow)
            {
                this.MetaInfoHandle(content);
                this.TopMenu.ButtonChecks(content);
            }
        }

        private void MetaInfoHandle(ViewModelBase content)
        {
            var metaInfoHolder = content as IMetaInfoHolder;
            if (metaInfoHolder != null)
            {
                this.MetaInfoBar.MetaInfos = metaInfoHolder.GetMetaInfoList();
                this.MetaInfoBar.IsVisible = true;
            }
            else
            {
                this.MetaInfoBar.MetaInfos = new List<MetaInfo>();
                this.MetaInfoBar.IsVisible = false;
            }

            var metaInfoUpdater = content as IMetaInfoUpdater;
            if (metaInfoUpdater != null)
            {
                metaInfoUpdater.MetaInfoUpdated += (sender, args) => this.MetaInfoBar.MetaInfos = args.UpdatedMetaInfos;
            }
        }
    }
}